<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Db;
use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property int $user_id 
 * @property int $task_id 
 * @property string $task_total 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 */
class TaskProgressDay extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'task_progress_days';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['user_id','task_id','task_total','created_at','updated_at'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'user_id' => 'integer', 'task_id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    /**
     * 更新日志记录
     * @param $user_id
     * @param $task_id
     * @param $task_total
     * @return bool
     */
    public static function addRecord($user_id, $task_id,$task_total)
    {
        $first = self::where(['user_id'=>$user_id,'task_id'=>$task_id])->where('created_at','>=',date('Y-m-d'))->first();
        if(empty($first)){
            self::create([
                'user_id' => $user_id,
                'task_id' => $task_id,
                'task_total' => Db::raw($task_total)
            ]);
        }else{
            self::where('id',$first['id'])->update([
                'task_total' => Db::raw($task_total)
            ]);
        }

    }

    /**
     * 获取当日用户完成任务的进度
     * @param $user_id
     */
    public static function getByUserID($user_id)
    {
        $time = date('Y-m-d H:i:s',strtotime(date('Y-m-d')));
        return self::where('user_id',$user_id)->where('created_at','>=',$time)->get()->toArray();
    }
}